// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>MyCompany.MyProject</Solution>
// <Project>MyCompany.MyProject.Core</Project>
// <FileName>ProductDomainService.cs</FileName>
// <CreateTime>2017-04-22 22:02</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-22 22:02" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Vecen.BangChuang.BangChuang.DomainServices.Agents;
using Vecen.BangChuang.BangChuang.DomainServices.EventHandlers.EventDatas;
using Vecen.BangChuang.BangChuang.Products;
using Abp.Runtime.Session;

namespace Vecen.BangChuang.BangChuang.DomainServices.Products {
    public class ProductDomainService: BangChuangDomainServiceBase {
        private readonly IRepository<Product> _productRepository;
        private readonly AgentDomainService _agentDomainService;

        public ProductDomainService(IRepository<Product> productRepository,
            AgentDomainService agentDomainService) {
            _productRepository = productRepository;
            _agentDomainService = agentDomainService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<IQueryable<Product>> GetProductsByKeyword(string keyword) {
            var query = _productRepository.GetAll();
            query = query.WhereIf(!keyword.IsNullOrWhiteSpace(), c => c.Name.Contains(keyword));
            await EventBus.TriggerAsync(new SearchEventData(keyword));
            return query;
        }

        [Obsolete]
        public async Task<decimal?> GetUserPriceByIdAsync(int productId) {
            var userType = await _agentDomainService.GetUserTypeAsync(AbpSession.GetUserId());
            return await GetUserPriceByIdAsync(productId, userType);
        }

        [Obsolete]
        public async Task<decimal?> GetUserPriceByIdAsync(int productId, string userType)
        {
            var product = await _productRepository.FirstOrDefaultAsync(productId);
            switch (userType)
            {
                case UserType.FirstAgentUser:
                case UserType.SecondAgentUser:
                    return product.AgentPrice;
                case UserType.GeneralUser:
                    return product.ExFactoryPrice;
                case UserType.InnerUser:
                    return product.CostPrice;
                default:
                    return 0;

            }
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteProductAsync(int id) {
            await EventBus.TriggerAsync(new ProductDeleteBeforeEventData() {ProductId = id});
            await _productRepository.DeleteAsync(id);
            await EventBus.TriggerAsync(new ProductDeleteAfterEventData() {ProductId = id});

        }


        /// <summary>
        /// 下架
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task OffTheShelfAsync(int id) {
            var product = await _productRepository.FirstOrDefaultAsync(id);
            product.Status = ProductStatus.不可售;
            await _productRepository.UpdateAsync(product);
            await EventBus.TriggerAsync(new ProductOfTheShelfEventData() {ProductId = id, OnOrOff = false});
        }

        /// <summary>
        /// 上架
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task OnTheShelfAsync(int id) {
            var product = await _productRepository.FirstOrDefaultAsync(id);
            product.Status = ProductStatus.可售;
            product.LastPutOnTime = DateTime.Now;
            await _productRepository.UpdateAsync(product);
            await EventBus.TriggerAsync(new ProductOfTheShelfEventData() {ProductId = id, OnOrOff = true});
        }
    }
}